package com.yunchao.library.socket.model.control

import com.yunchao.library.socket.extensions.getString
import com.yunchao.library.socket.extensions.putString
import com.yunchao.library.socket.extensions.toReadOnlyBuffer
import com.yunchao.library.socket.utils.BufferPool


data class DispatchRequest(
    val isOffline: Boolean = false,  // 是否离线消息
    val id: UInt,                    // 无符号ID (使用最高位作为离线标志)
    val timestamp: ULong,            // 无符号时间戳
    val data: String
) {
    companion object {
        // 常量定义
        private const val OFFLINE_FLAG_MASK = 0x80000000        // 最高位掩码
        private const val ID_NUMBER_MASK = 0x7FFFFFFF           // 低31位掩码

        fun fromByteArray(bytes: ByteArray): DispatchRequest {
            val buffer = bytes.toReadOnlyBuffer()
            // 读取并解析ID
            val rawId = buffer.int.toUInt()
            val isOffline = (rawId and OFFLINE_FLAG_MASK.toUInt()) != 0u
            val id = rawId and ID_NUMBER_MASK.toUInt()
            // 读取其他字段
            val timestamp = buffer.long.toULong()
            val data = buffer.getString()
            return DispatchRequest(isOffline, id, timestamp, data)

        }

        fun DispatchRequest.toByteArray(): ByteArray {
            // 计算所需缓冲区大小: 4(int) + 8(long) + 2(UTF长度前缀) + data字节长度
            val bufferSize = 4 + 8 + 2 + data.toByteArray(Charsets.UTF_8).size

            return BufferPool.use(bufferSize) { buffer ->
                // 写入组合ID
                val combinedId = if (isOffline) {
                    id or OFFLINE_FLAG_MASK.toUInt()
                } else {
                    id
                }

                // 写入组合后的ID
                buffer.putInt(combinedId.toInt())
                // 写入时间戳
                buffer.putLong(timestamp.toLong())
                buffer.putString(data)

                buffer.array().copyOf(buffer.position())
            }
        }
    }
}


/*


data class DispatchRequest(
    val isOffline: Boolean = false,  // 是否离线消息
    val id: Int,
    val timestamp: Long,
    val data: String
) {
    companion object {
        // 常量定义
        private const val OFFLINE_FLAG_MASK = 0x80000000.toInt()  // 最高位掩码 (1000 0000 ...)
        private const val ID_NUMBER_MASK = 0x7FFFFFFF              // 低31位掩码 (0111 1111 ...)

        fun fromByteArray(bytes: ByteArray): DispatchRequest {
            val buffer = bytes.toReadOnlyBuffer()
            val result = parseId(buffer.int.toUInt())
            return DispatchRequest(
                isOffline = result.first,
                id = result.second,
                timestamp = buffer.long,
                data = buffer.getString()
            )
        }

        */
/**
 * 使用ByteBuffer将DispatchRequest转换为字节数组
 *//*

        fun DispatchRequest.toByteArray(): ByteArray {
            // 计算所需缓冲区大小: 4(int) + 8(long) + 2(UTF长度前缀) + data字节长度
            val bufferSize = 4 + 8 + 2 + data.toByteArray(Charsets.UTF_8).size
            return BufferPool.use(bufferSize) { buffer ->
                // 写入组合后的ID
                buffer.putInt(buildId(isOffline, id).toInt())
                // 写入时间戳
                buffer.putLong(timestamp)
                buffer.putString(data)

                buffer.array().copyOf(buffer.position())
            }
        }


        */
/**
 * 解析U4类型的ID值
 * @param idValue 4字节无符号整数 (UInt)
 * @return Pair<是否脱机, 控制数据编号>
 *//*

        private fun parseId(idValue: UInt): Pair<Boolean, Int> {
            // 检查最高位判断状态
            val isOffline = (idValue.toInt() and OFFLINE_FLAG_MASK) != 0

            // 提取低31位编号
            val controlDataId = idValue.toInt() and ID_NUMBER_MASK

            return isOffline to controlDataId
        }


        */
/**
 * 构建U4类型的ID值
 * @param isOffline 是否脱机
 * @param controlDataId 控制数据编号 (0..2^31-1)
 * @return 组合后的U4值
 *//*

        fun buildId(isOffline: Boolean, controlDataId: Int): UInt {
            return if (isOffline) {
                (controlDataId or OFFLINE_FLAG_MASK).toUInt()
            } else {
                controlDataId.toUInt()
            }
        }
    }
}*/
